package com.kgc.itrip.component;

import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.util.Map;


@Component
@ConfigurationProperties(prefix = "spring.mail")
@Data
public class MailTools {
    @Resource
    private JavaMailSenderImpl mailSender;
    private String subject;
    @Resource
    private FreeMarkerConfigurer freeMarkerConfigurer;
    private String from;
    /**发送普通的电子邮件
     * mailContent 主题
     * to 发给谁*/
    public void sendSimpleEmail(String mailContent,String...to){
        this.sendSimpleEmail(subject,mailContent,to);
    }

    public void sendSimpleEmail(String subject,String mailContent,String...to){
        SimpleMailMessage mailMessage =new SimpleMailMessage();
        mailMessage.setSubject(subject);//主题设置
        mailMessage.setTo(to);//发给谁
        mailMessage.setFrom(from);//谁发的
        mailMessage.setText(mailContent);//邮件类容
        mailSender.send(mailMessage);
    }
    public void sendTemplateMail(String subject, Map<String,Object> model, String templatePath, String...to) throws IOException, TemplateException, MessagingException {
        Template mailtemplate = freeMarkerConfigurer.getConfiguration().getTemplate(templatePath);
        String templateHtml = FreeMarkerTemplateUtils.processTemplateIntoString(mailtemplate,model);
        this.sendMimeMail(subject,templateHtml,to);
    }

    private void sendMimeMail(String subject, String templateHtml, File[] files, String[] to) throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(templateHtml,true);
        if(files!=null&&files.length>0){
            for (File file:files){
                if(file!=null){
                    String filename = file.getName();
                    helper.addAttachment(filename,file);

                }
            }
        }
        mailSender.send(mimeMessage);
    }

    private void sendMimeMail(String subject, String templateHtml, String[] to) throws MessagingException {

        this.sendMimeMail(subject,templateHtml,null,to);

    }
    private void sendMimeMail( String templateHtml, String[] to) throws MessagingException {

        this.sendMimeMail(this.subject,templateHtml,null,to);

    }

}
